package com.ulrica.morning.tools;

import com.ulrica.morning.dto.tcmsp.DrugName;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * @author LongKui
 * @since 2022/06/29
 */
public class CN2Pinyin {
    public static void main(String[] args) {
        String s = "葛花、木香、砂仁、茯苓、猪苓、人参、白术、白豆蔻、青皮、陈皮、神曲、干姜、泽泻";
        Set<String> set = new HashSet<>(Arrays.asList(s.trim().split("、")));
        Set<DrugName> drugNameSet = batchGetPinyin(set);
        drugNameSet.forEach(System.out::println);

    }

    public static Set<DrugName> batchGetPinyin(Set<String> cnNameSet){
        Set<DrugName> drugNameSet = new HashSet<>();
//        cnNameSet = cnNameSet.stream().filter(StringUtils::isNotBlank).collect(Collectors.toSet());
        for (String cnName : cnNameSet) {
            if (StringUtils.isBlank(cnName)) {
                continue;
            }
            Set<String> pinyinSet = Pinyin4jUtil.getPinyin(cnName, true);
            pinyinSet = firstToUpperCase(pinyinSet);
            if (CollectionUtils.isEmpty(pinyinSet)) {
                System.out.printf("%s 转换拼音失败%n", cnName);
                continue;
            }
            DrugName drugName = DrugName.builder().name(cnName).pinyin(pinyinSet).build();
            drugNameSet.add(drugName);
        }
        return drugNameSet;
    }

    private static Set<String> firstToUpperCase(Set<String> pinyinSet) {
        Set<String> set = new HashSet<>();
        for (String pinyin : pinyinSet) {
            set.add(firstToUpperCase(pinyin));
        }
        return set;
    }

    public static String firstToUpperCase(String value) {
        char[] chars = value.toCharArray();
        if (97 <= chars[0] && chars[0] <= 122) {
            chars[0] ^= 32;
        }
        return String.valueOf(chars);
    }


}
